package com.gs.SynAntiWandering.adapter.base;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.gs.SynAntiWandering.R;
import com.gs.SynAntiWandering.control.InsightView;
import com.gs.SynAntiWandering.util.ImageUtil;

public class BaseViewHolder extends RecyclerView.ViewHolder {

    private Context context;
    private final SparseArray<View> mViews;
    private final View convertView;

    public BaseViewHolder(Context context, View itemView) {
        super(itemView);
        this.context = context;
        convertView = itemView;
        mViews = new SparseArray<>();
    }

    public static BaseViewHolder get(Context context, ViewGroup parent, int layoutId) {
        View itemView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        return new BaseViewHolder(context, itemView);
    }

    public <T extends View> T fd(int id) {
        View view = mViews.get(id);
        if (view == null) {
            view = convertView.findViewById(id);
            mViews.put(id, view);
        }
        return (T) view;
    }

    public void setImageResource(Context activity, int id, String resource) {
        ImageView iv = fd(id);
        ImageUtil.getInstance().loadImg(activity, resource, iv);
    }

    public void setPersonImageResource(Context activity, int id, String resource) {
        ImageView iv = fd(id);
        ImageUtil.getInstance().loadPersonImg(activity, resource, iv);
    }

    public void setImageResource(int id, int resource) {
        ImageView iv = fd(id);
        iv.setImageResource(resource);
    }

    public void setVisibility(int id, int visible) {
        View v = fd(id);
        v.setVisibility(visible);
        v.setOnClickListener(null);
    }

    public void setHint(int id, String text) {
        TextView tx = convertView.findViewById(id);
        tx.setText("");
        tx.setHint(text);
    }

    public void setText(int id, String text) {
        TextView tv = fd(id);
        tv.setText(text);
    }

    public void setInsight(int id, Integer num, String name, int position, BaseAdapter.OnItemChildrenClickListener onItemChildrenClickListener) {
        InsightView view = fd(id);
        view.setTitle(name);
        if (num == null) {
            view.setNum("-");
        } else {
            view.setNum(String.valueOf(num));
        }
        if ((position + 1) % 2 == 0) {
            view.setTextColor(R.color.red);
            view.setDrawable(R.drawable.oval_stoke_red_bg);
        } else {
            view.setTextColor(R.color.green);
            view.setDrawable(R.drawable.oval_stoke_green_bg);
        }
        if (onItemChildrenClickListener != null) {
            view.setOnClickListener(v -> onItemChildrenClickListener.onItemChildrenClickListener(v, position));
        }
    }

    public void setOnClickListener(int id, View.OnClickListener onClickListener) {
        View v = fd(id);
        v.setOnClickListener(onClickListener);
    }
}
